package kaug.p1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

/**
 * Main class.
 * @author Ago
 *
 */
public class Main {

	/**
	 * An example how to deal with "magic number" problem.
	 * If you have a number you use a lot in your code
	 * and which also has a meaning, you should define
	 * a constant and use it.
	 * <p>
	 * Now you can use:
	 * <code>
	 * if (answer == MEANING_OF_LIFE) </code>
	 * 
	 * instead of
	 * <code>
	 * if (answer == 42) </code>
	 * 
	 * The first one give more information for the reader,
	 * what exactly are you trying to compare (or do etc).
	 */
	public static final int MEANING_OF_LIFE = 42;
	
	/**
	 * The piece for the computer player. An example
	 * for tic-tac-toe game. Instead of comparison
	 * 
	 * <code>if (board[i] == "X") </code>
	 * 
	 * it would make more sense:
	 * <code>if (board[i] == COMPUTER_PLAYER) </code>
	 */
	public static final String COMPUTER_PLAYER = "X";
	/**
	 * This is the main method.
	 * @param args Command line arguments
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		System.out.println("Hello world!");
		System.out.println(MEANING_OF_LIFE);
		p("Tere");
		p(4);
		p(5);
		
		//int number = readNumber();
		//p("number:" + number); // number:5
		
		// Person[] persons = new Person[100];
		ArrayList<Person> persons = new ArrayList<Person>();
		
		while (true) {
			Person p = readPerson();
			if (p == null) {
				break;
			} else {
				persons.add(p);
			}
		}
		
		// "exit"
		// for (int i = 0; i < persons.size(); i++) {
		// Person p = persons.get(i);
		for (Person p : persons) {
			System.out.println(p);
		}
		
		// reading from the web
		String response = readFromWeb("http://translate.google.com/m?hl=en&sl=et&tl=en&ie=UTF-8&prev=_m&q=jou+maailm");
		System.out.println(response);
	}
	
	/**
	 * Prints a string.
	 * @param s String to be printed.
	 */
	public static void p(String s) {
		//String s;
		System.out.println(s);
	}
	
	/**
	 * Prints a number. Overloaded method
	 * (has the same name as with the string
	 * printing method, but arguments are different).
	 * @param a number to be printed
	 */
	public static void p(int a) {
		System.out.println(a);
	}
	
	
	/**
	 * Reads person's data (name and age)
	 * from standard input
	 * in the form &lt;name&gt;, &lt;age&gt;.
	 * (javadoc is formatted as html,
	 * therefore I cannot use less than and
	 * greated than symbols directly, but instead
	 * use entities &lt;&gt;).
	 * @return Person with name and age.
	 */
	public static Person readPerson() {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		/*
		 * 
		 * try {
		 * // statements - error
		 * // statement2
		 * } catch (Exception e) {
		 *   // if exception happened
		 *   
		 * } catch (Exception2 e) {
		 * }
		 * 
		 */

		while (true) {
			System.out.println("Enter <name>, <age>:");
			try {
				String line = br.readLine();
				if (line.equals("exit")) {
					// line == "exit"
					return null;
				}
				String[] tokens = line.split(",");
				if (tokens.length != 2) {
					// too few or too many commas
					continue; // goes to while(true)
				}
				// tokens[0] = <name>
				// tokens[1] = <age>
				String name = tokens[0].trim(); // mati   ,   12
				
				int age = Integer.parseInt(tokens[1].trim());
				
				/*Person p = new Person();
				p.name = name;
				p.age = age;
				*/
				Person p = new Person(name, age);
				
				return p;
				// break;
				// not reachable
			} catch (IOException e) {
				System.out.println("IOExceptin happened");
			} catch (NumberFormatException e) {
				System.out.println("not a number");
			}
		}
	}

	/**
	 * Reads a number from standard input.
	 * @return Number entered from console
	 */
	public static int readNumber() {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		/*
		 * 
		 * try {
		 * // statements - error
		 * // statement2
		 * } catch (Exception e) {
		 *   // if exception happened
		 *   
		 * } catch (Exception2 e) {
		 * }
		 * 
		 */
		
		System.out.println("Enter a number:");
		while (true) {
			try {
				String line = br.readLine();
				int a = Integer.parseInt(line);
				return a;
				// break;
				// not reachable
			} catch (IOException e) {
				System.out.println("IOExceptin happened");
			} catch (NumberFormatException e) {
				System.out.println("not a number");
			}
		}
	}
	
	/**
	 * Opens the link and reads all the lines from the stream.
	 * @param link Link to open
	 * @return Full response as one line (line-breaks 
	 * have been removed)
	 */
	public static String readFromWeb(String link) {
		String response = "";
		URL urlObject;
		try {
			urlObject = new URL(link);
			URLConnection c = urlObject.openConnection();
			c.setRequestProperty("User-agent", "Mozilla/5.0");
			BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
			String line;
			while ((line = br.readLine()) != null) {
				response += line;
			}
			return response;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// if we reached here, we had an exception
		// (otherwise the earlier return would have been executed).
		return null;
	}

}
